package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"path/filepath"
	"zg3zk3/api/request"
	"zg3zk3/interface/dao"
	"zg3zk3/interface/models"
	"zg3zk3/pkg"
)

func Zbj(context *gin.Context) {
	var req request.ZbjReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "code",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ui := context.GetUint("userId")
	fmt.Println(ui)
	var user models.Users
	dao.ZbjFind(ui, &user)
	if ui == 0 {
		context.JSON(500, gin.H{
			"code": "code",
			"msg":  "账号未登录",
			"data": nil,
		})
		return
	}

	zbj := models.Zbjs{
		HouseId: ui,
		Context: req.Context,
		Person:  req.Person,
		Img:     req.Img,
		Message: req.Message,
		Status:  req.Status,
	}

	dao.ZbjCreate(&zbj)

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "",
		"data": zbj,
	})

}

func ZbjUpdate(context *gin.Context) {
	var req request.ZbjReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "code",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	zbj := models.Zbjs{
		HouseId: context.GetUint("userId"),
		Context: req.Context,
		Person:  req.Person,
		Img:     req.Img,
		Message: req.Message,
		Status:  req.Status,
	}

	dao.ZbjUpdates(&zbj)

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "",
		"data": zbj,
	})

}

func ZbjUpload(context *gin.Context) {
	file, _ := context.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\goWork\\wenjian\\million\\zg3zk3\\static\\upload" + file.Filename
	pkg.Upload(dst)
	// 上传文件至指定的完整文件路径
	err := context.SaveUploadedFile(file, dst)
	if err != nil {
		return
	}
	ext := filepath.Ext(file.Filename)

	if ext != ".png" && ext != ".jpg" {
		context.JSON(200, gin.H{
			"code": "200",
			"msg":  "格式不能是png和jpg",
			"data": nil,
		})
		return
	}
	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "格式不能是png和jpg",
		"data": nil,
	})
}
